package ynu.lcy.service;

import cn.hutool.core.bean.BeanUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.lcy.dao.IDeliveryAddressDao;
import ynu.lcy.dto.delievryAddress.AddDeliveryAddressRequestDTO;
import ynu.lcy.dto.delievryAddress.UpdateDeliveryAddressRequestDTO;
import ynu.lcy.elememicroservicecommon.exception.BusinessException;
import ynu.lcy.elememicroservicecommon.utils.ErrorCode;
import ynu.lcy.elememicroserviceserviceclient.service.AuthFeignClient;

import ynu.lcy.entity.DeliveryAddress;
import ynu.lcy.entity.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

import static ynu.lcy.elememicroservicecommon.constant.CommonConstant.ACCESS_TOKEN;
import static ynu.lcy.elememicroservicecommon.utils.TokenUtils.getToken;

@Service
public class DeliveryAddressServiceImpl implements DeliveryAddressService {

    @Resource
    private IDeliveryAddressDao dao;
    @Resource
    private AuthFeignClient authFeignClient;

    /**
     * 根据用户获取地址
     *
     * @param request 请求
     * @return List<DeliveryAddress>
     */
    public List<DeliveryAddress> list(HttpServletRequest request) {
        Long loginUser = authFeignClient.getLoginUser(getToken(request));
        return dao.findAllByUserId(loginUser);
    }

    /**
     * 新建地址
     *
     * @param dto     请求参数
     * @param request 请求
     * @return DeliveryAddress
     */
    public DeliveryAddress save(AddDeliveryAddressRequestDTO dto, HttpServletRequest request) {
        DeliveryAddress address = new DeliveryAddress();
        BeanUtil.copyProperties(dto, address);
        Long loginUser = authFeignClient.getLoginUser(getToken(request));
        address.setUserId(loginUser);
        address.setIsDeleted(0);
        address.setCreateTime(new Date());
        return dao.save(address);
    }

    /**
     * 更新地址
     *
     * @param dto 请求参数
     * @return boolean
     *
     * */
    @Transactional
    public boolean update(UpdateDeliveryAddressRequestDTO dto) {
        try {
            if (dto.getId() <= 0) return false;
            // 更新
            DeliveryAddress address = dao.getReferenceById(dto.getId());
            BeanUtil.copyProperties(dto, address);
            dao.save(address);
            return true;
        } catch (
                Exception e
        ) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 根据id获取
     * @param id
     * @return
     * */
    public DeliveryAddress get(Long id) {
        return dao.getReferenceById(id);
    }
    /**
     * 删除地址
     *
     * @param id 地址id
     * @return boolean
     * */
    public  boolean delete(Long id) {
        if (id <= 0) return false;
        try {
            DeliveryAddress address = dao.getReferenceById(id);
            address.setIsDeleted(1);
            dao.save(address);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
